function CanvasTextWrap(context,TextObj){
if(TextObj.center=="center"){
	TextObj.x=TextObj.x+TextObj.width/2;
	TextObj.y=TextObj.y+TextObj.height/2;
}else if(TextObj.center=="right"){
	TextObj.x=TextObj.x+TextObj.width;
	TextObj.y=TextObj.y+TextObj.height;
}

var CanvasText = function(position, size) {
    this._textStack = [];

    if (position === null || position === undefined) {
        position = {
            x: 0,
            y: 0
        }
    }

    if (size === null || size === undefined) {
        size = {
            width: 0,
            height: 0
        }
    }

    this._position = position;
    this._size = size;

}

CanvasText.prototype = {
    _position: null,
    _size: null,
    _textStack: null,
    _currentOptionSet: null,

    _newOptionSet: function() {
        if (this._currentOptionSet != null) {
            return;
        }

        this._currentOptionSet = {
            text: '',
            family: '',
            size: '',
            weight: '',
            style: '',
            color: ''
        };
    },

    position: function(x, y) {
        this._position.x = x;
        this._position.y = y;
    },

    family: function(family) {
        this._newOptionSet();
        this._currentOptionSet.family = family;
        return this;
    },

    size: function(size) {
        this._newOptionSet();
        this._currentOptionSet.size = size;
        return this;
    },

    weight: function(weight) {
        this._newOptionSet();
        this._currentOptionSet.weight = weight;
        return this;
    },

    style: function(style) {
        this._newOptionSet();
        this._currentOptionSet.style = style;
        return this;
    },

    color: function(color) {
        this._newOptionSet();
        this._currentOptionSet.color = color;
        return this;
    },

    append: function(text) {
        this._newOptionSet();
        this._currentOptionSet.text = text;
        this._textStack.push(this._currentOptionSet);
        this._currentOptionSet = null;
        return this;
    },

    newLine: function() {
        this.append('\n');
        return this;
    },

    render: function() {

        if (this._textStack.length == 0) {
            return;
        }

        var previousFontOptions = {
            text: '',
            family: '',
            size: '',
            weight: '',
            style: ''
        };

        var textAdjustment = {
            x: 0,
            y: 0
        };

        context.save();

        for (var i = 0, textStackLen = this._textStack.length; i < textStackLen; i++) {

            var currentFontOptions = this._textStack[i];

            // we must store previous font options so we can override any single one of them without losing previously stored settings
            if (currentFontOptions.family) {
                previousFontOptions.family = currentFontOptions.family;
            }

            if (currentFontOptions.size) {
                previousFontOptions.size = currentFontOptions.size;
            }

            if (currentFontOptions.weight) {
                previousFontOptions.weight = currentFontOptions.weight;
            }

            if (currentFontOptions.style) {
                previousFontOptions.style = currentFontOptions.style;
            }

            context.font = jQuery.trim(previousFontOptions.weight + ' ' + previousFontOptions.style + ' ' + previousFontOptions.size + ' ' + previousFontOptions.family);

            // we don't need to store previous color, and can instead use the context to remember the previously used color
            if (currentFontOptions.color) {
                context.fillStyle = currentFontOptions.color;
            }
            
            var textToDraw = currentFontOptions.text;
            var wordsArray = textToDraw.split(' ');

            for (var j = 0, wordsArrayLen = wordsArray.length; j < wordsArrayLen; j++) {
                var currentWord = ' ' + wordsArray[j];
                var currentWordWidth = context.measureText(currentWord).width;

                // word wrap code here
                if (textAdjustment.x + currentWordWidth > this._size.width || textToDraw == '\n') {
                    textAdjustment.x = 0;
                    textAdjustment.y += parseInt(previousFontOptions.size, 10);
                }

                // we don't want to draw anything for new line characters, nor apply textAdjustment additions
                if (textToDraw == '\n') {
                    continue;
                }
				context.textAlign=TextObj.center;
                context.fillText(
                    currentWord,                            // text
                    this._position.x + textAdjustment.x,    // x position
                    this._position.y + textAdjustment.y     // y position

                );

                textAdjustment.x += currentWordWidth;
            }
        }

        //context.restore();
    }
}

var myText = new CanvasText({
    x: TextObj.x,
    y: TextObj.y
},{
    width: TextObj.width,
    height: TextObj.height
});

myText
    .family(TextObj.family)
    .size(TextObj.size)
    .weight(TextObj.weight)
    .color(TextObj.color)
    .style(TextObj.style)//斜体
    .append(TextObj.text)   
    .render();

    
}